package com.hyxt.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;

import java.util.Map;

/**
 *
 * <p>
 * redis缓存操作类
 * </p>
 * @author zhangcx
 * @创建时间：2017年11月16日15:38:50
 * @产品: 2.0Pass
 * @version： V1.0
 */
public class RedisOperationUtil {
    /**
     * 设置 String
     *
     * @param key
     * @param value
     */
    public synchronized static void setString(String key, String value,int dbIndex) {
        Jedis jedis = null;
        try {
            value = StringUtils.isEmpty(value) ? "" : value;
            jedis = RedisPoolUtil.getJedis(dbIndex);
            jedis.set(key, value);
        } catch (Exception e) {
            //logger.error("Set key error : " + e);
        }finally {
            RedisPoolUtil.returnResource(jedis);
        }
    }

    /**
     * 设置 过期时间
     *
     * @param key
     * @param seconds 以秒为单位
     * @param value
     */
    public synchronized static void setString(String key, int seconds, String value,int dbIndex) {
        Jedis jedis = null;
        try {
            value = StringUtils.isEmpty(value) ? "" : value;
            jedis = RedisPoolUtil.getJedis(dbIndex);
            jedis.setex(key, seconds, value);
        } catch (Exception e) {
            //logger.error("Set keyex error : " + e);
        }finally {
            RedisPoolUtil.returnResource(jedis);
        }
    }

    /**
     * 获取String值
     *
     * @param key
     * @return value
     */
    public synchronized static String getString(String key,int dbIndex) {
        Jedis jedis = null;
        jedis = RedisPoolUtil.getJedis(dbIndex);
        if (jedis == null || !jedis.exists(key)) {
            return null;
        }
        //returnResource(getJedis());
        return jedis.get(key);
    }
    /**
     * 向缓存中设置对象
     * @param key
     * @param value
     * @return
     */
    public static boolean  setObject(String key,Object value,int dbIndex){
        Jedis jedis = null;
        try {
            String objectJson = JSON.toJSONString(value);
            jedis = RedisPoolUtil.getJedis(dbIndex);
            jedis.set(key, objectJson);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally{
            RedisPoolUtil.returnResource(jedis);
        }
    }

    /**
     * 删除缓存中得对象，根据key
     * @param key
     * @return
     */
    public static boolean deleteObject(String key,int dbIndex){
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis(dbIndex);
            jedis.del(key);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally{
            RedisPoolUtil.returnResource(jedis);
        }
    }

    /**
     * 根据key 获取内容
     * @param key
     * @return
     */
    public static Object getObject(String key,int dbIndex){
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis(dbIndex);
            Object value = jedis.get(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally{
            RedisPoolUtil.returnResource(jedis);
        }
    }


    /**
     * 根据key 获取对象
     * @param key
     * @return
     */
    public static <T> T getObject(String key,Class<T> clazz,int dbIndex){
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis(dbIndex);
            String value = jedis.get(key);
            return JSON.parseObject(value, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally{
            RedisPoolUtil.returnResource(jedis);
        }
    }

    /**
     * 向对应的频道发送消息
     * @param channel
     * @param message
     */
    public static void sendMessage(String channel,String message){
        if(channel != null && !"".equals(channel) && message !=null){
            try {
                RedisPoolUtil.getJedis().publish(channel,message);
            }finally {
                RedisPoolUtil.getJedis().close();
            }

        }
    }

    /**
     * 存储Map值
     * @param key  redis关键字
     * @param map   map value参数
     * @param dbIndex  redisDB索引
     * @return
     */
    public static boolean setMap(String key, Map<String,String> map,Integer dbIndex){
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis(dbIndex);
            jedis.hmset(key,map);
            return true;
        }catch (Exception e){
            return false;
        }finally {
            RedisPoolUtil.returnResource(jedis);
        }
    }

    /**
     * 获得Map值
     * @param key  redis关键字
     * @param dbIndex  redisDB索引
     * @return
     */
    public static Map<String,String> getMap(String key,Integer dbIndex){
        Jedis jedis = null;
        try {
            jedis = RedisPoolUtil.getJedis(dbIndex);
            return jedis.hgetAll(key);
        }catch (Exception e){
            return null;
        }finally {
            RedisPoolUtil.returnResource(jedis);
        }
    }
}
